package com.hnkywl.sts.action.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.sys.Bm;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.cggl.CgdService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.cwgl.YwcbService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.organ.QyjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.XsdService;
import com.hnkywl.sts.util.POIUtil;
import com.hnkywl.sts.util.ReflectionUtils;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/ywcb")
public class YwcbAction extends SimpleActionSupport {
    private YwcbService ywcbService;

    private String xsdh;
    private Date fhsjStart;
    private Date fhsjEnd;
    private Long xshtId;
    private Long khId;
    private Long gcId;
    private Long sbgsId;
    private Long sbbmId;
    private Long sbrId;
    private String cxzt;
    private String zt;
    private String sftzd;
    private Boolean dlkhxsd=true;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    private Date currentTime = new Date();

   //导入界面字段
    private Yg yg;
    private String gsmc;
    private String systemDate;
    private XsdDao xsdDao;
    private Long ywcbId;
    private YgService ygService;
    private Map<String, String> result = new HashMap<String, String>();
    private Integer attachment;

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();

    //导入部分字段
    private VoucherService voucherService;
    private Map<String, Object> voucher;
    private XmlParse xmlParse;
    private Date ywcbSystemDate;
    private Gson gson = new Gson();


    @Action("ywcb")
    public String xsdgl() {
        fhsjStart = sDaysAfterNowDate(1);
        fhsjEnd = sDaysAfterNowDate(0);
        return SUCCESS;
    }


    @Action("ywcbList")
    public void ywcbList() throws ParseException {
        this.renderJson(this.ywcbService.findAllYwcb(xsdh, fhsjStart, fhsjEnd, xshtId, khId, gcId, sbgsId, sbbmId, sbrId, zt, cxzt, sftzd, null,dlkhxsd, page, rows));
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    private Date sDaysAfterNowDate(int n) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DAY_OF_MONTH, -n);
        return rightNow.getTime();
    }

    //贴现承兑进入票据页面
    @Action("ywcb_insert")
    public String ywcbInsert(){
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsmc = xsdDao.get(ywcbId).getSbgs().getJc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date()) ;
        voucherMaster = this.ywcbService.getVoucherMaster(ywcbId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    @Action("findywcbfl")
    public void findYwcbFl(){
        renderText(ywcbService.findQtpzfl(ywcbId));
    }

    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有U8用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有U8密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(ywcbId, VoucherType.YWCB);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
            voucher = this.ywcbService.getU8VoucherItems(ywcbId, uuid, null,ywcbSystemDate);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.ywcbService.getVoucherMaster(ywcbId,null, uuid,null,null,yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate",simpleDateFormat.format(ywcbSystemDate));
            voucherMaster.put("acctyear",simpleDateFormat2.format(ywcbSystemDate));
            voucherMaster.put("acctperiod",simpleDateFormat3.format(ywcbSystemDate));
            voucherMaster.put("attachment",attachment);
            voucherMaster.put("memo", VoucherType.YWCB.getTag()+ywcbId);//备注，在凭证删除时使用
            this.ywcbService.ywcbChangeDrrq(ywcbId, ywcbSystemDate);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, ywcbId, VoucherType.YWCB);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            voucherService.checkJiuqiUsernamePassword();
            Boolean begin = voucherService.beginRevokeVoucher(ywcbId, VoucherType.YWCB);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = this.ywcbService.getRevokeInfo(ywcbId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            revokeInfo.put("memo", VoucherType.YWCB.getTag()+ywcbId);//备注，在凭证删除时使用
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(ywcbId, VoucherType.YWCB);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
        renderJson(voucherService.restVoucher(ywcbId, VoucherType.YWCB));
    }




    public String getXsdh() {
        return xsdh;
    }

    public void setXsdh(String xsdh) {
        this.xsdh = xsdh;
    }

    public Date getFhsjStart() {
        return fhsjStart;
    }

    public void setFhsjStart(Date fhsjStart) {
        this.fhsjStart = fhsjStart;
    }

    public Date getFhsjEnd() {
        return fhsjEnd;
    }

    public void setFhsjEnd(Date fhsjEnd) {
        this.fhsjEnd = fhsjEnd;
    }

    public Long getXshtId() {
        return xshtId;
    }

    public void setXshtId(Long xshtId) {
        this.xshtId = xshtId;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public Long getGcId() {
        return gcId;
    }

    public void setGcId(Long gcId) {
        this.gcId = gcId;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public Long getSbbmId() {
        return sbbmId;
    }

    public void setSbbmId(Long sbbmId) {
        this.sbbmId = sbbmId;
    }

    public Long getSbrId() {
        return sbrId;
    }

    public void setSbrId(Long sbrId) {
        this.sbrId = sbrId;
    }

    public String getCxzt() {
        return cxzt;
    }

    public void setCxzt(String cxzt) {
        this.cxzt = cxzt;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public Boolean getDlkhxsd() {
        return dlkhxsd;
    }

    public void setDlkhxsd(Boolean dlkhxsd) {
        this.dlkhxsd = dlkhxsd;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Date getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    public Long getYwcbId() {
        return ywcbId;
    }

    public void setYwcbId(Long ywcbId) {
        this.ywcbId = ywcbId;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Date getYwcbSystemDate() {
        return ywcbSystemDate;
    }

    public void setYwcbSystemDate(Date ywcbSystemDate) {
        this.ywcbSystemDate = ywcbSystemDate;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    @Autowired
    public void setYwcbService(YwcbService ywcbService) {
        this.ywcbService = ywcbService;
    }
    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
    @Autowired
    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }
}
